package E10.ebrestful;

import E10.UploadFile;
import E10.util.TokenMaker;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import maven.Scope;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 史祥连
 * @Date 2024/1/29 9:29
 * @Version 1.0
 */
public class Add {
    private static final String URL_DEV = "http://127.0.0.1:18092";
    private static final String URL = "http://192.168.51.122:19092";
    private static final String Tok = "PCACCOUNT_3606d35a03ef8e0f3ad303b1674c1c73";
    private static final String USER= "5116064451991577795";
    private static final String PK= "f1b697";

    public static void main(String[] args) {
        JSONArray datas = new JSONArray();

        JSONObject data = new JSONObject();
        JSONObject main = new JSONObject();
        main.put("jarmc","com.weaver:weaver-ebuilder-form-common");
        data.put("mainTable",main);
        Add add = new Add();
        datas.add(data);
        String s = TokenMaker.loadAccessToken();
        add.doAction(s,datas,"n95776");
        /*main.put("scope", Scope.compile.name());
        main.put("sfzjyl",1);*/

    }


    public static JSONObject getMainTable(JSONObject map) {
        JSONObject main = new JSONObject();
        main.put("jarmc","com.weaver:weaver-ebuilder-form-common");
        main.put("scope", Scope.compile.name());
        main.put("sfzjyl",1);
        return main;

    }

    public static String addData(JSONArray object,String token,String pk) {
        if (token == null) {
            token = UploadFile.loadAccessToken();
        }
        Add saveDemo = new Add();
        String result = saveDemo.doAction(token,object, pk);
        return result;
    }


    /**
     * e-Builder openApi Demo
     * ex:getFormDataList
     * @param accessToken
     */
    public String doAction(String accessToken,JSONArray datas,String pk) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(URL+"/api/ebuilder/form/formdata/v2/saveFormData/"+pk+"?access_token=" + accessToken);
        JSONObject params = new JSONObject();
        JSONObject paramDatajson = new JSONObject();

        //header
        JSONObject header = new JSONObject();
        header.put("operator", USER);
        paramDatajson.put("header", header);



        //mainTable
        //operationinfo
        JSONObject operationinfo = new JSONObject();
        operationinfo.put("optionNoFindIgnore",false);
        //datas
        paramDatajson.put("datas",datas);

        params.put("datajson", paramDatajson);

        System.out.println("===request datajson===" + paramDatajson);

        List nvps = new ArrayList();
        if (params != null) {
            for (Map.Entry entry : params.entrySet()) {
                nvps.add(new BasicNameValuePair((String) entry.getKey(), JSONObject.toJSONString(entry.getValue())));
            }
        }
        try {
            UrlEncodedFormEntity reqEntity = new UrlEncodedFormEntity(nvps, "utf-8");
            httpPost.setEntity(reqEntity);
            // exec post request.
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != 200) {
                    System.out.println("request failed");
                    throw new RuntimeException("request failed");
                } else {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        System.out.println("request success");
                        return EntityUtils.toString(entity, Charset.forName("UTF-8"));
                    }
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";

    }
    public static String null2String(Object s) {
        return s == null ? "" : s.toString();
    }
}
